<?php
/**
 * 文章分类Model
 * */
namespace app\admin\model;

use think\Model;
use think\Db;


class Category extends Model
{


    //自定义初始化
    protected function initialize()
    {
        parent::initialize();
        $this->db = db('category');
    }

    /**
     * 所有分类数据
     * 数据ID作为键值
     * */
    public function categoryData(){
        $order = 'sort asc,id';
        $res = $this->db->cache(config('data_expire'))->order($order)->select();
       // 格式化数据  ID作为键值
        foreach($res as $k =>$v){
            $data[$v['id']]['id']                    = $v['id'];
            $data[$v['id']]['pid']                  = $v['pid'];
            $data[$v['id']]['name']               = $v['name'];
            $data[$v['id']]['extend']             = $v['extend'];
            $data[$v['id']]['desc']                = $v['desc'];
            $data[$v['id']]['is_delete']           = $v['is_delete'];
            $data[$v['id']]['sort']                  = $v['sort'];
            $data[$v['id']]['add_time']           = $v['add_time'];
        }
        return $data;
    }

    /**
     * 单条数据详情
     * @param array $input 表单提交过来的数据
     * @return array 返回单条数据
     * */
    public function info($id){
        $res = $this->categoryData();
        // 判断$id ,是否存在数组中
       if(array_key_exists($id,$res)){
            return $res[$id];
        }else{
            return false;
        }
        return $res;
    }

    /**
     * 单条数据添加
     * @param array $data 需要插入的数组
     * @return string  1 或者0
     * */
    public function categoryInsert($data){
        $res = db('category')->insert($data);
        return $res;
    }

    /**
     * 单条数据更新
     * @param int $id 传递的ID
     * @param array $data 需要更新的数组
     * @return  int 1 or 0
     * */
    public function categoryUpdate($id,$data){
        $where['id'] = $id;
        $res = $this->db->where($where)->update($data);
        return $res;
    }

    /**
     * 单条数据删除
     * @param  int $id 删除id
     * @return string 1 or 0
     * */
    public  function categoryDelete($id){
        // 判断分类是否可以删除
        $result = $this->categoryData();

        /* 减除检测 */

        //1.子类，禁止删除
        $count = $this->pidData($id);
        if($count){
            return 0;return false;
        }
        //2.传递id是否存在数组中
        if(array_key_exists($id,$result)){
            // 3.默认分类禁止删除
            if($result[$id]['is_delete'] ==1){
                $str ='0';
            }else{
                // 4.执行删除
                $str = $this->db->delete($id);
            }
        }else{
            $str = '0';
        }
        return $str;
    }




    /**
     * 子分类列表
     * */
    public function pidData($pid=null){
        $result ='';
        $arr = $this->categoryData();
        foreach($arr as $k=>$v){
            if($v['pid'] == $pid){
                $result[$v['id']] = $v;
            }
        }
        return $result;
    }

    /**
     * 无限分类树
     * */
    public function tree($pid=0){
        $res = $this->pidData($pid);
        if(empty($res)) return false;
        foreach($res as $k=>$v){
            $result[$v['id']] = $v;

            $rf = $this->tree($v['id']);
            if($rf){
                foreach($rf as $k=>$vv){
                    $result[$vv['id']]['id'] = $vv['id'];
                    $result[$vv['id']]['name'] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|- ".$vv['name'];
                    $result[$vv['id']]['pid'] = $vv['pid'];
                    $result[$vv['id']]['desc'] = $vv['desc'];
                    $result[$vv['id']]['extend'] = $vv['extend'];
                    $result[$vv['id']]['sort'] = $vv['sort'];
                    $result[$vv['id']]['is_delete'] = $vv['is_delete'];
                    $result[$vv['id']]['add_time'] = $vv['add_time'];
                }
            }
        }
        return $result;
    }


    /**
     * 把分类变成结构树
     * @param array $items 数组
     * @return  结构树
     * */
    function getTree($items,$pid ="pid") {
        $map  = [];
        $tree = [];
        foreach ($items as &$it){ $map[$it['id']] = &$it; }  //数据的ID名生成新的引用索引树
        foreach ($items as &$it){
            $parent = &$map[$it[$pid]];
            if($parent) {
                $parent['son'][] = &$it;
            }else{
                $tree[] = &$it;
            }
        }
        return $tree;
    }

    /**
     * 无限分类转opt格式
     * @param int $val 选中的ID
     * @return string  option字符串
     * */
    public function treeOpt($val=null){
        $str = '';
        $arr = $this->tree(0);
        foreach($arr as $k=>$v){
            if($v['id'] == $val){
                $str .="<option value=$v[id] selected>".$v['name']."</option>";
            }else{
                $str .="<option value=$v[id] >".$v['name']."</option>";

            }
        }
        return $str;
    }









}